//
//  JYShopController.m
//  JYShop
//
//  Created by 花花 on 2017/9/28.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYShopController.h"
#import "JYShopCell.h"
#import "JYShopCartViewModel.h"
#import "JYShopCartBottomView.h"
#import "JYShopCartTableViewProxy.h"
#import "JYCommitOrderController.h"
#import "JYShowView.h"
#import <MJRefresh/MJRefresh.h>
@interface JYShopController ()<JYShopCartViewModelDelegate>
@property(nonatomic)JYShopCartTopView *shopcartTopView;  // 购物车顶部视图

@property(nonatomic)UITableView *tableView;  //购物车列表

@property(nonatomic)JYShopCartBottomView *shopcartBottomView;  // 购物车底部视图

@property (nonatomic) JYShopCartViewModel *viewModel;   // 负责购物车逻辑处理

@property (nonatomic)JYShopCartTableViewProxy *shopcartTableViewProxy; //tableView代理

@end
@implementation JYShopController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupSubview];
    [self setupConstranints];
    [self loadDataFromNetwork];
    
    
    [self setupRefresh];

}
- (void)setupRefresh{

    JYWeakSelf;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       
        [weakSelf loadDataFromNetwork];
        
    }];

}

- (void)loadDataFromNetwork{
    
    [self.tableView.mj_header endRefreshing];
    
    [self.viewModel requestShopcartProductList];
    
}
- (void)setupSubview{
    
    [self.view addSubview:self.shopcartTopView];
    
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.shopcartBottomView];
    
}
#pragma mark - <JYShopCartViewModelDelegate>
//获取数据
- (void)shopcartRequestDataSuccessWithArray:(NSMutableArray *)dataArray{
    
    self.shopcartTableViewProxy.dataArray = dataArray;
    
    [self.tableView reloadData];
    
}
- (void)shopcartAccountForTotalPrice:(float)totalPrice totalCount:(double)totalCount isAllSelected:(BOOL)isAllSelected{
    
    [self.shopcartBottomView configureShopcartBottomViewWithTotalPrice:totalPrice totalCount:totalPrice isAllselected:isAllSelected];
    
    [self.tableView reloadData];
}

//提交订单
- (void)shopcartCommitOrderForSelectedProducts:(NSArray *)selectedProducts{
    
    JYWeakSelf;
    NSLog(@"-------%@",selectedProducts);
    if (selectedProducts.count == 0) {
        
        
        UIView *bgView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        bgView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.5];
        
        [[UIApplication sharedApplication].keyWindow addSubview:bgView];
        JYNoGoodsShowView*showView =  [[JYNoGoodsShowView alloc]initWithFrame:CGRectMake((Screen_Width - showView_width)/2, (Screen_Height- showView_height)/2, showView_width, showView_height)];
        [bgView addSubview:showView];
        
        [showView showText:@"您未选择任何商品，请选择商品后进行购买" onClickHandler:^{
            [bgView removeFromSuperview];
            [showView removeFromSuperview];
        }];
    }else if (selectedProducts.count>0){
        
        [weakSelf.navigationController pushViewController:[JYCommitOrderController new] animated:YES];
        
        
    }
    
}

//批量删除
- (void)shopcartWillDeleteSelectedProducts:(NSArray *)selectedProducts{
    
    
    if (selectedProducts.count>0) {
        
        UIView *bgView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        bgView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.5];
        
        [[UIApplication sharedApplication].keyWindow addSubview:bgView];
        JYShowView *showView = [[JYShowView alloc] initWithFrame:CGRectMake((Screen_Width - showView_width)/2, (Screen_Height- showView_height)/2, showView_width, showView_height)];
        [bgView addSubview:showView];
        JYWeakSelf;
        
        
        [showView showText:@"删除后商品将不在显示，确认删除类商品吗?" isSected:YES btnHandler:^(JYShowType type) {
            
            if (type == JYShowTypeCancel) {
                
                [bgView removeFromSuperview];
                [showView removeFromSuperview];
                
            }else{
                
                [bgView removeFromSuperview];
                [showView removeFromSuperview];
                
                [self.viewModel deleteSelectedProducts:selectedProducts];
                
                
            }
            
        }];
    }
    
}

//清空
- (void)shopcartClearDeleteAllProducts{
    
    [self.tableView reloadData];
    
}
- (void)setupConstranints{
    
    JYWeakSelf;
    [_shopcartTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(weakSelf.view);
        make.height.equalTo(@50);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@50);
        make.left.right.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.shopcartBottomView.mas_top);
    }];
    
    [self.shopcartBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(weakSelf.view);
        make.height.equalTo(@50);
    }];
}

- (JYShopCartTopView *)shopcartTopView {
    if (_shopcartTopView == nil){
        _shopcartTopView = [[JYShopCartTopView alloc] init];
        JYWeakSelf;
        _shopcartTopView.HandlerDeleteBtnOnClick=^(){ //删除
            [weakSelf.viewModel beginToDeleteSelectedProducts];
            
        };
        _shopcartTopView.HandlerEmptyBtnOnClick =^(){ //清空
            
            [weakSelf.viewModel deleteAllProducts];
        };
    }
    return _shopcartTopView;
}

- (UITableView *)tableView {
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"JYShopCell" bundle:nil] forCellReuseIdentifier:@"JYShopCellID"];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self.shopcartTableViewProxy;
        _tableView.dataSource = self.shopcartTableViewProxy;
        _tableView.rowHeight = 140;
        _tableView.sectionFooterHeight = 10;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    }
    return _tableView;
}

- (JYShopCartViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel =[[JYShopCartViewModel alloc]init];
        _viewModel.delegate = self;
    }
    return _viewModel;
    
}
- (JYShopCartTableViewProxy *)shopcartTableViewProxy {
    if (_shopcartTableViewProxy == nil){
        JYWeakSelf;
        _shopcartTableViewProxy = [[JYShopCartTableViewProxy alloc] init];
        _shopcartTableViewProxy.ShopCartHandlerSelectBlock = ^(BOOL isSelected,NSIndexPath *indexPath){ //选中
            
            [weakSelf.viewModel selectProductAtIndexPath:indexPath isSelected:isSelected];
        };
        _shopcartTableViewProxy.ShopCartHandlerDeleteBlock = ^(NSIndexPath *indexPath){ //左滑删除
            
            
            UIView *bgView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
            bgView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.5];
            
            [[UIApplication sharedApplication].keyWindow addSubview:bgView];
            JYShowView *showView = [[JYShowView alloc] initWithFrame:CGRectMake((Screen_Width - showView_width)/2, (Screen_Height- showView_height)/2, showView_width, showView_height)];
            [bgView addSubview:showView];
            
            [showView showText:@"删除后商品将不在显示，确认删除类商品吗?" isSected:YES btnHandler:^(JYShowType type) {
                
                if (type == JYShowTypeCancel) {
                    
                    [bgView removeFromSuperview];
                    [showView removeFromSuperview];
                    
                }else{
                    
                    [bgView removeFromSuperview];
                    [showView removeFromSuperview];
                    
                    [weakSelf.viewModel deleteProductAtIndexPath:indexPath];
                }
            }];
        };
        _shopcartTableViewProxy.ShopCartHandlerChangeCountBlock = ^(NSInteger count, NSIndexPath *indexPath){ //数量的变化
            
            [weakSelf.viewModel changeCountAtIndexPath:indexPath count:count];
        };
    }
    return _shopcartTableViewProxy;
}

- (JYShopCartBottomView *)shopcartBottomView {
    if (_shopcartBottomView == nil){
        _shopcartBottomView = [[JYShopCartBottomView alloc] init];
        JYWeakSelf;
        _shopcartBottomView.HandlerAllBtnOnClick = ^(BOOL isSelected){ //全选
            
            [weakSelf.viewModel selectAllProductWithStatus:isSelected];
        };
        _shopcartBottomView.HandlerCommitOrderBtnOnClick = ^(){ //提交订单
            
            [weakSelf.viewModel commitOdrerSelectedProducts];
        };
    }
    return _shopcartBottomView;
}
@end
